package controller

import (
	"mini-app/internal/app/web/service"
	"strconv"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type GoodsReview struct{}

// Create 新增评测
func (GoodsReview) Create(c *gin.Context) {
	var params struct {
		OrderID string `json:"order_id"`
		Star    string `json:"star"`
		Content string `json:"content"`
		Images  string `json:"images"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	id, err := service.GoodsReview{}.Create(
		c.Request.Context(),
		c.GetString("openid"),
		params.OrderID,
		params.Star,
		params.Content,
		params.Images,
	)
	response.JSON(c, gin.H{
		"id": id,
	}, err)
}

// Update 更新评测
func (GoodsReview) Update(c *gin.Context) {
	var params struct {
		ID      string `json:"id"`
		Star    string `json:"star"`
		Content string `json:"content"`
		Images  string `json:"images"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.GoodsReview{}.Update(
		c.Request.Context(),
		c.GetString("openid"),
		params.ID,
		params.Star,
		params.Content,
		params.Images,
	)
	response.JSON(c, nil, err)
}

// Detail 评测详情
func (GoodsReview) Detail(c *gin.Context) {
	detail, err := service.GoodsReview{}.Detail(
		c.Request.Context(),
		c.Query("id"),
		c.Query("order_id"),
	)
	response.JSON(c, detail, err)
}

func (GoodsReview) List(c *gin.Context) {
	status, _ := strconv.Atoi(c.Query("status"))
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total, err := service.GoodsReview{}.List(
		c.Request.Context(),
		c.GetString("openid"),
		status,
		page, limit,
	)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// NearbyReviews 附近评测
func (GoodsReview) NearbyReviews(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total := service.GoodsReview{}.NearbyReviews(
		c.Request.Context(),
		page, limit,
	)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}
